package com.sviet.game.tetriswindow;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.Log;

import com.sviet.game.tetriswindow.data.GlobalVariable;
import com.sviet.game.tetriswindow.util.ITetrisConstants;

public class TetrisHud implements ITetrisConstants {
	
	public static void paintRightHud(Context context, Canvas canvas, Paint paint, int right,
			int top, int nextType) {
		int x, y;

		// score
		x = right;
		y = top + context.getResources().getDimensionPixelSize(R.dimen.hub_score_y_start);
		paint.setTextAlign(Align.RIGHT);
		
		int textSize = context.getResources().getDimensionPixelSize(R.dimen.text_size);
//		int textSize = 24;
		paint.setTextSize(textSize);

		paint.setColor(HUD_SCORE_WORD_COLOR);
		canvas.drawText(context.getString(R.string.level), x, y, paint);
		y += context.getResources().getDimensionPixelSize(R.dimen.hub_score_interline);
		paint.setColor(HUD_SCORE_NUM_COLOR);
//		int level = SharedPreferencesUtils.GetPreference(context, SharedPreferencesUtils.LEVEL);
		canvas.drawText(String.valueOf(GlobalVariable.level), x, y, paint);

		y += context.getResources().getDimensionPixelSize(R.dimen.hud_next_word_y_start);
		paint.setColor(HUD_SCORE_WORD_COLOR);
		canvas.drawText(context.getString(R.string.line), x, y, paint);
		y += context.getResources().getDimensionPixelSize(R.dimen.hub_score_interline);
		paint.setColor(HUD_SCORE_NUM_COLOR);
		canvas.drawText("" + GlobalVariable.current_number_line, x, y, paint);

		y += context.getResources().getDimensionPixelSize(R.dimen.hud_next_word_y_start);
		paint.setColor(HUD_SCORE_WORD_COLOR);
		canvas.drawText(context.getString(R.string.score), x, y, paint);
		y += context.getResources().getDimensionPixelSize(R.dimen.hub_score_interline);
		paint.setColor(HUD_SCORE_NUM_COLOR);
		canvas.drawText("" + GlobalVariable.current_score, x, y, paint);

		// next shape
		displayNextShape(context, canvas, paint, x, y, Align.RIGHT, nextType);

		// top scores
//		displayTopScores(canvas, paint, s.getTopScores(), right, top,
//				Align.RIGHT);

		// normal align
		paint.setTextAlign(Align.LEFT);
	}

	public static void displayNextShape(Context context, Canvas canvas, Paint mPaint,
			int startX, int startY, Align a, int nextType) {
		int tmpX, tmpY, offset;
		int i = 0;
		int x = startX;
		int y = startY + context.getResources().getDimensionPixelSize(R.dimen.hud_next_word_y_start);

		mPaint.setTextAlign(a);
		mPaint.setColor(HUD_NEXT_WORD_COLOR);
//		mPaint.setTextSize(24);
		canvas.drawText("Next", x, y, mPaint);
		
		if (nextType == -1)
		{
			return;
		}
		
		mPaint.setColor(HUD_NEXT_SHAPE_COLOR);
		x = startX - context.getResources().getDimensionPixelSize(R.dimen.hud_next_shape_x_start);
		y += context.getResources().getDimensionPixelSize(R.dimen.hub_score_interline);
		tmpX = x;
		tmpY = y;
		Log.v("", "nextType = " + nextType);
		offset = (nextType * SHAPE_TABLE_TYPE_OFFSET) + START_ORIENTATION
				* SHAPE_TABLE_ELEMS_PER_ROW;
		int hud_next_shape_cell_offset = context.getResources().
				getDimensionPixelSize(R.dimen.hud_next_shape_cell_offset);
		do {
			canvas.drawRect(tmpX, tmpY, tmpX + context.getResources().
					getDimensionPixelSize(R.dimen.hud_next_shape_cell_size), tmpY
					+ context.getResources().
					getDimensionPixelSize(R.dimen.hud_next_shape_cell_size), mPaint);
			switch (SHAPE_TABLE[offset + i]) {
			case C_LEFT:
				tmpX = x - hud_next_shape_cell_offset;
				tmpY = y;
				break;
			case C_RIGHT:
				tmpX = x + hud_next_shape_cell_offset;
				tmpY = y;
				break;
			case C_UP:
				tmpX = x;
				tmpY = y - hud_next_shape_cell_offset;
				break;
			case C_DOWN:
				tmpX = x;
				tmpY = y + hud_next_shape_cell_offset;
				break;
			case C_LEFT + C_DOWN:
				tmpX = x - hud_next_shape_cell_offset;
				tmpY = y + hud_next_shape_cell_offset;
				break;
			case C_RIGHT + C_DOWN:
				tmpX = x + hud_next_shape_cell_offset;
				tmpY = y + hud_next_shape_cell_offset;
				break;
			case C_LEFT + C_UP:
				tmpX = x - hud_next_shape_cell_offset;
				tmpY = y - hud_next_shape_cell_offset;
				break;
			case C_RIGHT + C_UP:
				tmpX = x + hud_next_shape_cell_offset;
				tmpY = y - hud_next_shape_cell_offset;
				break;
			case C_RIGHT * 2:
				tmpX = x - (hud_next_shape_cell_offset * 2);// i am cheating
															// here and moving
															// to the left for
															// better hud
															// display (anchor
															// is on right)
				tmpY = y;
				break;
			default:
				// need to manage
				break;
			}
			i++;
		} while (i < MAX_ELEMS);
	}

//	public static void displayTopScores(Canvas canvas, Paint p, Cursor c,
//			int x, int y, Align a) {
//
//		x -= HUD_TOP_SCORES_TEXT_OFFSET;
//		y += HUD_TOP_SCORES_Y_START;
//		String name;
//		int score;
//		int colScore = c
//				.getColumnIndex(ScoreManager.DATABASE_TABLE_SCORES_SCORE);
//		int colName = c.getColumnIndex(ScoreManager.DATABASE_TABLE_SCORES_NAME);
//
//		c.moveToLast();
//		p.setTextAlign(a);
//		// paint title
//		p.setColor(HUD_TOP_SCORES_TITLE_COLOR);
//		canvas.drawText("TopScore", x, y, p);
//		y += 20;
//
//		for (int i = 0; i < c.getCount() && i < ScoreManager.TOP_SCORE_NB; i++, c
//				.moveToPrevious(), y += 14) {
//
//			score = c.getInt(colScore);
//			name = c.getString(colName);
//			p.setColor(HUD_TOP_SCORES_RANK_COLOR);
//			canvas.drawText((i + 1) + ". " + name + ": " + score, x, y, p);
//		}
//		c.close();
//
//	}
}
